*First import and rename state demographic characteristics as of 1960 to merge on later
clear all
use "C:\Users\ot3\Documents\CRFertility\CensusBooks\DigCountyDataBooks\ICPSR_07736\DS0001\07736-0001-Data.dta" 
keep if FIPSCNTY=="000" //state aggregates

rename FIPSTATE state_bir
destring state_bir, replace
rename CC00003 landarea1960
rename CC00014 totpop1960
rename CC00134 mededuc1960 
rename CC00243 medinc1960
rename CC00030 netmig1960_1970
rename CC00039 pcturban1960
rename CC00055 pctblack1960

keep state_bir landarea1960 totpop1960 mededuc1960 medinc1960 netmig1960_1970 pcturban1960 pctblack1960
cd "C:\Users\ot3\Documents\CRFertility\CensusMicrodata"
save statechars1960, replace


*Now import and clean up the Census/ACS sample from IPUMS
cd "C:\Users\ot3\Documents\CRFertility\CensusMicrodata"

clear
quietly infix                ///
  int     year      1-4      ///
  long    sample    5-10     ///
  double  serial    11-18    ///
  double  cbserial  19-31    ///
  double  hhwt      32-41    ///
  double  cluster   42-54    ///
  byte    statefip  55-56    ///
  double  strata    57-68    ///
  byte    gq        69-69    ///
  byte    gqtype    70-70    ///
  int     gqtyped   71-73    ///
  int     pernum    74-77    ///
  double  perwt     78-87    ///
  byte    sex       88-88    ///
  int     age       89-91    ///
  byte    marst     92-92    ///
  int     birthyr   93-96    ///
  byte    race      97-97    ///
  int     raced     98-100   ///
  byte    hispan    101-101  ///
  int     hispand   102-104  ///
  int     bpl       105-107  ///
  long    bpld      108-112  ///
  byte    school    113-113  ///
  byte    educ      114-115  ///
  int     educd     116-118  ///
  byte    empstat   119-119  ///
  byte    empstatd  120-121  ///
  byte    labforce  122-122  ///
  byte    wkswork1  123-124  ///
  byte    wkswork2  125-125  ///
  byte    hrswork1  126-127  ///
  byte    hrswork2  128-128  ///
  byte    uhrswork  129-130  ///
  long    inctot    131-137  ///
  long    incwage   138-143  ///
  using `"usa_00078.dat"'

replace hhwt     = hhwt     / 100
replace perwt    = perwt    / 100

format serial   %8.0f
format cbserial %13.0f
format hhwt     %10.2f
format cluster  %13.0f
format strata   %12.0f
format perwt    %10.2f

*sample restrictions
drop if school==2 //current students
keep if age>=25 & age<=54
keep if bpl<=66 //abroad, outlying territories, missings

recode race (1=0)(2=1)(else=.), gen(black)
recode sex (1=0)(2=1), gen(fem)

*Education
recode educ (0=0)(1=2)(2=6)(3=9)(4=10)(5=11)(6=12)(7=13)(8=14)(9=15)(10=16)(11=18), gen(hgc)

*Specify the "nursery school to grade 4" in the years the more specific number is available
replace hgc=0 if hgc==2 & (educd==11 | educd==12)
replace hgc=1 if hgc==2 & educd==14
replace hgc=2 if hgc==2 & educd==15 
replace hgc=3 if hgc==2 & educd==16
replace hgc=4 if hgc==2 & educd==17
*same for "Grade 5, 6, 7, or 8" 
replace hgc=5 if hgc==6 &  educd==22
replace hgc=6 if hgc==6 & educd==23
replace hgc=7 if hgc==6 & (educd==24 | educd==25)
replace hgc=8 if hgc==6 & educd==26


*Earnings
replace incwage=. if incwage>=999998
cpigen
replace cpi=1.36 if year==2013
replace cpi=1.39 if year==2014
replace cpi=1.38 if year==2015
replace cpi=1.40 if year==2016
replace cpi=1.44 if year==2017
replace cpi=1.47 if year==2018
replace cpi=1.49 if year==2019
replace cpi=cpi/1.49 //makes 2019 the base year instead of2000, the cpigen default
replace incwage=incwage/cpi

*merge on fertility rates from Vital Statistics
rename bpl fipsst //for merging vital stats
rename year year_surv //for merging vital stats
rename birthyr year
merge m:1 fipsst black year using C:\Users\ot3\Documents\CRFertility\BirthData\frate_working_state
keep if _merge==3
rename year cohort
rename fipsst state_bir
rename statefip state_res


*collapse, reshape, and calculate black-white gaps in fertility and outcome by state-cohort-race
collapse (mean) brate incwage  hgc age year_surv fem (count) n=perwt [aweight=perwt], by(black state_bir cohort)
reshape wide brate incwage hgc age year_surv fem n, i(state_bir cohort) j(black)

local vars="hgc incwage brate "
foreach x of local vars  {
g BWGap_`x'=`x'0-`x'1
}

replace BWGap_brate=BWGap_brate/20
g log_incgap=ln(incwage0)-ln(incwage1)

*merge on 1960 state demographics from above
merge m:1 state_bir  using statechars1960
keep if _merge==3
drop _merge


g south= inlist(state_bir, 1, 5, 12, 13, 22, 28, 37,  45, 47, 48, 51) 
drop if BWGap_hgc==. | log_incgap==.

save census_twfe, replace










